package com.example.sps.controller;

import com.example.sps.commons.R;
import com.example.sps.commons.ServiceCode;
import com.example.sps.commons.ServiceException;
import com.example.sps.utils.AliossUtils;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;

/**
 * @ProjectName: Sps
 * @Titile: CommonController
 * @Author: nency
 * @Description: TODO
 */
@RestController
@RequestMapping("api/common")
@Slf4j
public class CommonController {
    @Autowired
    private AliossUtils aliossUtils;

    @PostMapping("/load")
    public R upload(MultipartFile file){
        try {
//            String originalFilename = file.getOriginalFilename();
//            String extend = originalFilename.substring(originalFilename.lastIndexOf("."));
//            String objectName = UUID.randomUUID().toString() + extend;
            String filePath = aliossUtils.upload(file.getBytes(), file);
            return R.ok("ok",filePath);
        }catch (IOException e){
            log.info("文件上传失败, {}",e);
        }
        return R.fail(new ServiceException(ServiceCode.ERROR_UPLOAD,"文件上传失败"));
    }
}
